import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'Login',
      component: () => import('@/views/Login.vue'),
      meta: { requiresAuth: false }
    },
    {
      path: '/register',
      name: 'Register',
      component: () => import('@/views/Register.vue'),
      meta: { requiresAuth: false }
    },
    {
      path: '/forgot-password',
      name: 'ForgotPassword',
      component: () => import('@/views/ForgotPassword.vue'),
      meta: { requiresAuth: false }
    },
    {
      path: '/reset-password',
      name: 'ResetPassword',
      component: () => import('@/views/ResetPassword.vue'),
      meta: { requiresAuth: false }
    },
    {
      path: '/',
      name: 'Layout',
      component: () => import('@/layout/MainLayout.vue'),
      meta: { requiresAuth: true },
      redirect: '/home',
      children: [
        {
          path: '/home',
          name: 'Home',
          component: () => import('@/views/Home.vue'),
          meta: { requiresAuth: true }
        },
        {
          path: '/create-couple',
          name: 'CreateCouple',
          component: () => import('@/views/CreateCouple.vue'),
          meta: { requiresAuth: true }
        },
        {
          path: '/join-couple',
          name: 'JoinCouple',
          component: () => import('@/views/JoinCouple.vue'),
          meta: { requiresAuth: true }
        },
        {
          path: '/balance',
          name: 'WechatBalance',
          component: () => import('@/views/WechatBalance.vue'),
          meta: { requiresAuth: true }
        },
        {
          path: '/bill/create',
          name: 'CreateBill',
          component: () => import('@/views/CreateBill.vue'),
          meta: { requiresAuth: true }
        },
        {
          path: '/bill/list',
          name: 'BillList',
          component: () => import('@/views/BillList.vue'),
          meta: { requiresAuth: true }
        },
        {
          path: '/bill/:id',
          name: 'BillDetail',
          component: () => import('@/views/BillDetail.vue'),
          meta: { requiresAuth: true }
        },
        {
          path: '/bill/pending',
          name: 'PendingBills',
          component: () => import('@/views/PendingBills.vue'),
          meta: { requiresAuth: true }
        },
        {
          path: '/treasury',
          name: 'Treasury',
          component: () => import('@/views/Treasury.vue'),
          meta: { requiresAuth: true }
        },
        {
          path: '/notification',
          name: 'NotificationCenter',
          component: () => import('@/views/NotificationCenter.vue'),
          meta: { requiresAuth: true }
        },
        {
          path: '/diary',
          name: 'DiaryList',
          component: () => import('@/views/DiaryList.vue'),
          meta: { requiresAuth: true }
        },
        {
          path: '/diary/create',
          name: 'CreateDiary',
          component: () => import('@/views/CreateDiary.vue'),
          meta: { requiresAuth: true }
        },
        {
          path: '/diary/:id',
          name: 'DiaryDetail',
          component: () => import('@/views/DiaryDetail.vue'),
          meta: { requiresAuth: true }
        },
        {
          path: '/notes',
          name: 'NoteList',
          component: () => import('@/views/NoteList.vue'),
          meta: { requiresAuth: true }
        },
        {
          path: '/anniversary',
          name: 'AnniversaryList',
          component: () => import('@/views/AnniversaryList.vue'),
          meta: { requiresAuth: true }
        },
        {
          path: '/achievement',
          name: 'AchievementList',
          component: () => import('@/views/AchievementList.vue'),
          meta: { requiresAuth: true }
        },
        {
          path: '/profile',
          name: 'ProfileSettings',
          component: () => import('@/views/ProfileSettings.vue'),
          meta: { requiresAuth: true }
        }
      ]
    }
  ]
})

// 路由守卫：检查登录状态
router.beforeEach((to, from, next) => {
  // 获取 token
  const token = localStorage.getItem('token')

  // 判断路由是否需要登录
  if (to.meta.requiresAuth) {
    // 需要登录
    if (token) {
      // 已登录，放行
      next()
    } else {
      // 未登录，跳转到登录页
      next('/login')
    }
  } else {
    // 不需要登录，直接放行
    // 如果已登录且访问登录页，跳转到首页
    if (token && (to.path === '/login' || to.path === '/register')) {
      next('/home')
    } else {
      next()
    }
  }
})

export default router
